package com.molyfun.parents.activity.setting;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.rest.BaseResponse;
import com.molyfun.parents.utils.PreferencesStorageUtil;
import com.molyfun.parents.view.SinglineClearEditText;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EEmailActivity extends BaseActivity implements View.OnClickListener {


    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.e_email_num)
    SinglineClearEditText mEEmail;
    @InjectView(R.id.confirmation_email)
    TextView mConfirmationEmail;
    @InjectView(R.id.base_title)
    TextView mTitle;

    public static void startActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, EEmailActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_e_email);
        ButterKnife.inject(this);
        mTitle.setText("电子邮件");
        initData();

    }

    private void initData() {
        getSupportActionBar().setTitle("");
        mBack.setOnClickListener(this);
        mConfirmationEmail.setOnClickListener(this);
        String email = PreferencesStorageUtil.getEmail(this);
        if(!TextUtils.isEmpty(email)) {
            mEEmail.setHint(email + "(点击更换)");
        }else{
            mEEmail.setHint("请输入您的电子邮箱");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.confirmation_email:
                //TODO 提交数据
                String mEEmailNum = mEEmail.getString();
                if (isEmail(mEEmailNum)) {
                    getConfirmationEmail(mEEmailNum);
                    finish();
                } else {
                    toast("邮箱有误");
                }
                break;
        }

    }

    private void getConfirmationEmail(final String mEEmailNum) {
        showProgressDialog();
        Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().signUpEmail(mEEmailNum);
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getMsg() != null) {
//                    toast(response.body().getMsg());
                    toast("邮箱更改成功");
                    PreferencesStorageUtil.setEmail(EEmailActivity.this,mEEmailNum);
                } else {
                    toast("提交失败");
                }
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }
    public static boolean isEmail(String email) {
        String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();

    }
}
